using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using ScriptEase.Shell.Controls;

namespace ScriptEase.Shell.Commands
{
    internal class GetLatestOverwriteCommand : ICommand
    {
        private readonly ShellViewModel _shellViewModel;
        private readonly WebResourceStatusView _viewModel;



        public GetLatestOverwriteCommand(ShellViewModel shellViewModel, WebResourceStatusView viewModel)
        {
            _shellViewModel = shellViewModel;
            _viewModel = viewModel;
        }



        public void Execute(object parameter)
        {
            MappingModel[] selectedItems = _viewModel.SelectedItems.Where(
                item => item.SyncStatus != FileSyncStatus.Publishing
                && item.SyncStatus != FileSyncStatus.Updating).ToArray();

            IEnumerable<string> selectedWebResourceNames = from item in selectedItems select item.WebResourceName;
            _shellViewModel.Project.UpdateLocal(selectedWebResourceNames.ToArray(), ConflictResolver.OverrideLocal);
        }



        public bool CanExecute(object parameter)
        {
            return (_viewModel.SelectedItems.Any(item => item.SyncStatus != FileSyncStatus.Publishing
                                                       && item.SyncStatus != FileSyncStatus.Updating));
        }



        public event EventHandler CanExecuteChanged;
    }
}